//
//  NumericKeypadView.swift
//  KoiPrototype2
//
//  Created by 纪洪文 on 08.03.25.
//

import SwiftUI

struct NumericKeypadView: View {

    @State private var showingAlert = false
    @State var doneText = "完成"
    @StateObject var calc = Calc()
    var numericClickCallback:(String)->Void
    var doneClickCallback:()->Void
    @Binding var reset: Bool
    @Binding var preAmount: String
    @State var preAmountOnce: Bool = true
    
    init(numericClickCallback: @escaping (String) -> Void, doneClickCallback: @escaping () -> Void, reset: Binding<Bool>,  preAmount: Binding<String>? = nil) {
        self.numericClickCallback = numericClickCallback
        self.doneClickCallback = doneClickCallback
        self._reset = reset
        self._preAmount = preAmount ?? .constant("")
    }
    
    var body: some View {
        GeometryReader { g in
            VStack (alignment: .center, spacing: 10){
                let height = (g.size.height-30)/4
                HStack (alignment: .center,spacing: 10) {
                    numericButton(text: "1")
                    numericButton(text: "2")
                    numericButton(text: "3")
                    numericButton(text: "+")
                }
                HStack (alignment: .center,spacing: 10){
                    numericButton(text: "4")
                    numericButton(text: "5")
                    numericButton(text: "6")
                    numericButton(text: "-")
                }
                HStack (alignment: .center,spacing: 10){
                    numericButton(text: "7")
                    numericButton(text: "8")
                    numericButton(text: "9")
                    ZStack {
                        Color.green.opacity(0)
                    }.overlay(content: {
                        Button (action: {
                            if (doneText == "=") {
                                doneText = "完成"
                                let result = calc.calc()
                                numericClickCallback(result)
                            } else {
                                doneClickCallback()
                            }
                        }){
                            ZStack {
                                Color.red.cornerRadius(16)
                                Text(doneText).foregroundColor(Color.white).bold().font(.system(size: 28))
                            }.frame(height: height*2+10).offset(x:0,y:height/2+5)
                        }.alert("overlay view click",isPresented: $showingAlert){
                            Text("aaa")
                        }
                    })
                }
                HStack (alignment: .center,spacing: 10){
                    Button(action: {
                        calc.input(text: ".")
                        numericClickCallback(calc.formulaContent)
                    }) {
                        ZStack{
                            Color.white.cornerRadius(16)
                            Color.black.frame(width: 8, height: 8).cornerRadius(15)}
                    }
                    
                    numericButton(text: "0")
                    Button(action:{
                        calc.backspace()
                        if (!calc.hasSymbol()) {
                            doneText = "完成"
                        }
                        if (calc.formulaContent.isEmpty) {
                            numericClickCallback("0.00")
                        } else {
                            numericClickCallback(calc.formulaContent)
                        }
                    }) {
                        ZStack{
                            Color.white.cornerRadius(16)
                            Image("koi_ic_backspace")}
                    }
                    
                    ZStack {
                        Color.green.opacity(0)
                    }
                }
            }
            .onChange(of: reset) { value in
                if value {
                    calc.reset()
                    reset = false
                }
            }
            .frame(width: g.size.width).background(Color.koiColorBackgroundItem).padding(0)
        }
    }
    
    private func numericButton(text:String) -> some View {
        Button(action:{
            // + - 号的特殊处理
            if (preAmountOnce && text == "+" || text == "-") && !preAmount.isEmpty {
                calc.input(text: preAmount)
                calc.input(text: text)
            } else {
                calc.input(text: text)
            }
            preAmountOnce = false
            if (calc.hasSymbol()) {
                doneText = "="
            }
            numericClickCallback(calc.formulaContent)
        }) {
            ZStack {
                Color.white.cornerRadius(16)
                Text(text)
                    .font(.system(size: 24))
                    .bold()
                    .foregroundColor(Color.koiColorTitle)
            }
        }
    }
}

@available(iOS 18, *)
#Preview {
    @Previewable @State var reset: Bool = false
    NumericKeypadView(numericClickCallback:{ text in }, doneClickCallback: { }, reset: $reset)
}
